package com.harth.flutter_ns_ble


fun CMD.toInt(): Int {
    return when (this) {
        CMD.MAGIC_CODE -> 0xbc
        CMD.INFO -> 0x10
        CMD.IMEI -> 0x20
        CMD.IMSI -> 0x21
        CMD.MEAS_TAG -> 0x22
        CMD.MEAS_FLASH -> 0x23
        CMD.MEAS_STATUS -> 0x24
        CMD.MEAS_COUNT -> 0x25
        CMD.MEAS_SN -> 0x26
        CMD.MEAS_FLASH_COUNT -> 0x27
        CMD.MEAS_INDEX -> 0x28
        CMD.R_GSTSET -> 0x28
        CMD.R_GSTGET -> 0x29
        CMD.ECG_CTL -> 0x30
        CMD.ECG_DATA -> 0x31
        CMD.ECG_STATUS -> 0x32
        CMD.DFU_INIT -> 0x40
        CMD.DFU_PACKET -> 0x41
        CMD.DFU_CHECK -> 0x42
        CMD.DFU_END -> 0x43
        CMD.UNKNOW -> 0x00
        CMD.WRITE_FACT_SN -> 0x55
        CMD.IOS -> 0x2a
        CMD.ANDROID -> 0x2b
        CMD.STOP_SYNC -> 0x2c
        CMD.TIME_CACHE -> 0x2d
    }
}

fun Int.toCMD(): CMD {
    return CMD.entries.firstOrNull { it.toInt() == this } ?: CMD.UNKNOW
}

fun ByteArray.toHex(separator: CharSequence = ", ") =
    joinToString(separator) { String.format("%02X", it) }

fun Byte.toBinaryString(): String {
    return String.format("%8s", Integer.toBinaryString(this.toInt() and 0xFF)).replace(' ', '0')
}